package net.adetech.crj.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.entity.Announcement;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.Notice;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OuterApplyWebController {

    @Autowired
    ServiceUtil serviceUtil;

    @RequestMapping(value = "/outerApply/page/add")
    public String addOuterApplyPage() {
        return "outerApply";
    }


    @RequestMapping(value = "/outerApply/page/remind")
    public String outerApplyRemindPage(Model model) {
        ServiceResponse response = serviceUtil.callService("notice.getByType", "type", 5);
        Notice notice = (Notice) response.getResults().get("notice");
        model.addAttribute("notice", notice);
        return "outerApplyRemind";
    }


    @RequestMapping(value = "/outerApply/page/submit")
    public String submitOuterApplyPage(String formJson, HttpSession session, Model model) {

        ZggmtbForm form = JSON.parseObject(formJson, ZggmtbForm.class);
        User user = (User) session.getAttribute("currentUser");
//        if(form.getQwd()!=null&&form.getQwd().size()==1)
//        {
//            form.setQwd(Arrays.asList(form.getQwd().get(0).split(",")));
//        }
//        if(form.getSqlb()!=null&&form.getSqlb().size()==1)
//        {
//            form.setSqlb(Arrays.asList(form.getSqlb().get(0).split(",")));
//        }

        ServiceRequest request = new ServiceRequest();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("form", form);
        params.put("user", user);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("outerApply.submit", request);


        List<Apply> applyList = (List<Apply>) response.getResults().get("applyList");
        JSONArray errorList = (JSONArray) response.getResults().get("errorList");
        model.addAttribute("applyList", applyList);

        for (int i = 0; i < errorList.size(); i++) {
            JSONObject errorJson = errorList.getJSONObject(i);
            String sqlb = errorJson.getString("sqlb");
            String message = errorJson.getString("message");
            message = message.substring(message.indexOf("Exception")+10);
            if ("102".equals(sqlb)) {
                model.addAttribute("gaError", message);
            } else if ("104".equals(sqlb)) {
                model.addAttribute("twError", message);

            }
        }


        for (Apply apply : applyList) {
            if ("OGA".equals(apply.getApplySummary())) {
                model.addAttribute("gaApply", apply);
            }
            if ("OTW".equals(apply.getApplySummary())) {
                model.addAttribute("twApply", apply);
            }
        }

        if (errorList.size()<=0)
        {
            model.addAttribute("iconClass", "weui-icon-success weui-icon_msg");
        }else if (applyList.size()<=0)
        {
            model.addAttribute("iconClass", "weui-icon-warn weui-icon_msg");

        }
        else {
            model.addAttribute("iconClass", "weui-icon-info weui-icon_msg");
        }


        return "outerApplyResult";
    }
}
